package com.lan.java8.model.info;

import com.lan.java8.antlr.Java8Parser;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class Field {

    private String name;

    private String type;

    private String typeImport;

    public static List<Field> get(Java8Parser.FieldDeclarationContext context) {
        Java8Parser.UnannTypeContext unannTypeContext = context.unannType();
        Java8Parser.VariableDeclaratorListContext variableDeclaratorListContext = context.variableDeclaratorList();

        if (unannTypeContext == null || variableDeclaratorListContext == null) {
            return null;
        }
        List<Java8Parser.VariableDeclaratorContext> variableDeclaratorContextList = variableDeclaratorListContext.variableDeclarator();
        if (variableDeclaratorContextList == null || variableDeclaratorContextList.size() == 0) {
            return null;
        }

        List<Field> fieldList = new ArrayList<>();
        for (int i = 0; i < variableDeclaratorContextList.size(); i++) {
            Field field = new Field();
            Java8Parser.VariableDeclaratorContext declaratorContext = variableDeclaratorContextList.get(i);
            field.setType(unannTypeContext.getText());
            field.setName(declaratorContext.getText());
            fieldList.add(field);
        }

        return fieldList;
    }
}
